package com.health.ts.controller.consultation;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.health.ts.constant.ApiResult;
import com.health.ts.controller.BaseController;
import com.health.ts.entity.sysmanage.SysDepartment;
import com.health.ts.entity.sysmanage.SysHospital;
import com.health.ts.entity.sysmanage.SysUser;
import com.health.ts.service.sysmanage.SysDepartmentService;
import com.health.ts.service.sysmanage.SysHospitalService;
import com.health.ts.service.sysmanage.SysUserService;
import com.health.ts.utils.SessionUserUtil;
/**
 * 会诊相关资源接口
 * @author Administrator
 */
@RestController
@RequestMapping(value = "/api/consultation",produces= BaseController.JSON_UTF8)
public class ConsultationSourceController extends BaseController{
	
	@Autowired
	SysHospitalService sysHospitalService;
	
	@Autowired
	SysDepartmentService sysDepartmentService;
	
	@Autowired
	SysUserService sysUserService;
	
	//用户报告角色
	private static final String USER_ROLE_REPORT="role_report";
	
	/**
	 * 查询申请会诊时指定医院列表
	 * @return
	 */
	@RequestMapping(value = "/hospitals/assign",method=RequestMethod.GET)
	public ApiResult listAssignHospitals() {
		QueryWrapper<SysHospital> wrapper = new QueryWrapper<SysHospital>();
		wrapper.orderByDesc("update_time");
		List<SysHospital> list=sysHospitalService.list(wrapper);
		return ApiResult.success(list);
	}
	
	/**
	 * 查询申请会诊时指定科室列表
	 * @return
	 */
	@RequestMapping(value = "/departments/assign",method=RequestMethod.GET)
	public ApiResult listAssignDepartments(String hospital_id) {
		QueryWrapper<SysDepartment> wrapper = new QueryWrapper<SysDepartment>();
		if(StringUtils.isNotEmpty(hospital_id)){
			wrapper.eq("hospital_id", hospital_id);
		}
		wrapper.orderByDesc("update_time");
		List<SysDepartment> list=sysDepartmentService.list(wrapper);
		return ApiResult.success(list);
	}
	
	/**
	 * 查询申请会诊时指定医生列表
	 * @return
	 */
	@RequestMapping(value = "/doctors/assign",method=RequestMethod.GET)
	public ApiResult listAssignDoctors(String hospital_id,String department_id) {
		QueryWrapper<SysUser> wrapper = new QueryWrapper<SysUser>();
		if(StringUtils.isNotEmpty(hospital_id)){
			wrapper.eq("hospital_id", hospital_id);
		}
		if(StringUtils.isNotEmpty(department_id)){
			wrapper.eq("department_id", department_id);
		}
		//返回结果需要用户角色列表包含报告角色的用户
		wrapper.like("role_id", USER_ROLE_REPORT);
		
		wrapper.orderByDesc("update_time");
		List<SysUser> list=sysUserService.list(wrapper);
		return ApiResult.success(list);
	}
	
	/**
	 * 查询会诊分配时指定医院列表
	 * @return
	 */
	@RequestMapping(value = "/hospitals/allot",method=RequestMethod.GET)
	public ApiResult listAllotHospitals() {
		QueryWrapper<SysHospital> wrapper = new QueryWrapper<SysHospital>();
		//会诊分配时只能分配用户自己医院下属资源
		wrapper.eq("id", SessionUserUtil.getUserHosId());
		wrapper.orderByDesc("update_time");
		List<SysHospital> list=sysHospitalService.list(wrapper);
		return ApiResult.success(list);
	}
	
	/**
	 * 查询会诊分配时指定科室列表
	 * @return
	 */
	@RequestMapping(value = "/departments/allot",method=RequestMethod.GET)
	public ApiResult listAllotDepartments(String hospital_id) {
		QueryWrapper<SysDepartment> wrapper = new QueryWrapper<SysDepartment>();
		if(StringUtils.isNotEmpty(hospital_id)){
			wrapper.eq("hospital_id", hospital_id);
		}
		
		wrapper.orderByDesc("update_time");
		List<SysDepartment> list=sysDepartmentService.list(wrapper);
		return ApiResult.success(list);
	}
	
	/**
	 * 查询会诊分配时指定医生列表
	 * @return
	 */
	@RequestMapping(value = "/doctors/allot",method=RequestMethod.GET)
	public ApiResult listAllotDoctors(String hospital_id,String department_id) {
		QueryWrapper<SysUser> wrapper = new QueryWrapper<SysUser>();
		if(StringUtils.isNotEmpty(hospital_id)){
			wrapper.eq("hospital_id", hospital_id);
		}
		if(StringUtils.isNotEmpty(department_id)){
			wrapper.eq("department_id", department_id);
		}
		//返回结果需要用户角色列表包含报告角色的用户
		wrapper.like("role_id", USER_ROLE_REPORT);
		
		wrapper.orderByDesc("update_time");
		List<SysUser> list=sysUserService.list(wrapper);
		return ApiResult.success(list);
	}
	
	@RequestMapping(value = "/doctors/{department_id}",method=RequestMethod.GET)
	public ApiResult listAssignDoctors(String department_id) {
		QueryWrapper<SysUser> wrapper = new QueryWrapper<SysUser>();
		if(StringUtils.isNotEmpty(department_id)){
			wrapper.eq("department_id", department_id);
		}
		wrapper.orderByDesc("update_time");
		wrapper.ne("user_type", "0");//非管理员
		wrapper.like("role_id", "role_report");//仅报告医生有费用
		List<SysUser> list=sysUserService.list(wrapper);
		return ApiResult.success(list);
	}
}